<?php
namespace app\admin\controller;

use app\admin\model\Comment as CommentModel;
use think\Request;
use think\Exception;

class Comment extends Common
{
    public function index()
    {
        $data = [];
        //搜索
        $telephone = input('post.telephone');
        $data['telephone'] = $telephone;
        $company_name = input('post.company_name');
        $data['company_name'] = $company_name;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = new CommentModel;
        //列表数据
        $array = [];
        $list = $model->getList($pageNum, $numPerPage);
        foreach ($list as $key => $item) {
            if(!empty($item['images'])){
                $item['images'] = explode(',',$item['images']);
                foreach ($item['images'] as $k => $v) {
                    $array[] = str_replace('\\','/',Request::instance()->domain() . DS . 'upload' . DS . 'api' . DS . 'image' . DS . $v);
                }
            }else{
                $list[$key]['images'] = [];
            }
            $list[$key]['images'] = $array;
        }

        $data['list'] = $list;
        //数据总数
        $count = $model->getCount();
        $data['count'] = $count;

        return view('', $data);
    }

    // 详情
    public function detail()
    {
        $id = input('get.id');
        $info = CommentModel::detail($id);
        if($info['status'] == 0){
            $info['status'] = '隐藏';
        }else{
            $info['status'] = '显示';
        }

        if(!empty($info['images'])){
            $info['images'] = explode(',',$info['images']);
            foreach ($info['images'] as $key => $item) {
                $info['images'][$key] = str_replace('\\','/',Request::instance()->domain() . DS . 'upload' . DS . 'api' . DS . 'image' . DS . $item);
            }
        }else{
            $info['images'] = [];
        }
        $data['info'] = $info;
        return view('', $data);
    }

    public function on_off()
    {
        $userId = input('get.id');
        $statusid = input('get.status');
        if ($statusid == 0) {
            $data['id'] = $userId;
            $data['status'] = 0;
            $status = "隐藏";
        }
        if ($statusid == 1) {
            $data['id'] = $userId;
            $data['status'] = 1;
            $status = "显示";
        }
        try {
            $Comment = new \app\admin\model\Comment();
            $Comment->editStatus($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message'    => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message'    => $status . '成功',
            'navTabId'   => 'CommentManage',
        ]);
    }

}